package consumer.json_utils;
import java.io.IOException;
import java.text.ParseException;
import java.util.List;

import io.confluent.kafka.consumer.entities.event.EventModel;
import org.codehaus.jackson.map.ObjectMapper;

/**
 * Created by taotao on 2017/12/18.
 */
public class Json2Java {
    public static void main(String[] args) throws ParseException, IOException {
        //String json = "{\"severity\":\"low\",\"alarm_name\":\"InstanceOne-alarm event-compute.instance.power_on.end\",\"current\":\"alarm\",\"alarm_id\":\"e1814804-104b-4080-a084-8320f70f3cb8\",\"reason\":\"Event <id=6a799ba5-b56a-47c2-8d72-5bc10b4b0dab,event_type=compute.instance.power_on.end> hits the query <query=[{\\\"field\\\": \\\"traits.instance_id\\\", \\\"op\\\": \\\"eq\\\", \\\"type\\\": \\\"string\\\", \\\"value\\\": \\\"940087d5-f889-4533-a5be-9574af5dd880\\\"}, {\\\"field\\\": \\\"traits.state\\\", \\\"op\\\": \\\"eq\\\", \\\"type\\\": \\\"string\\\", \\\"value\\\": \\\"active\\\"}]>.\",\"reason_data\":{\"type\":\"event\",\"event\":{\"event_type\":\"compute.instance.power_on.end\",\"traits\":[[\"user_id\",1,\"0e143fbbb51a4d2fb9e0076d07ff4362\"],[\"service\",1,\"compute\"],[\"disk_gb\",2,1],[\"resource_id\",1,\"940087d5-f889-4533-a5be-9574af5dd880\"],[\"tenant_id\",1,\"c679d411213b4ce086210d9412f2da90\"],[\"root_gb\",2,1],[\"ephemeral_gb\",2,0],[\"instance_type_id\",2,6],[\"state\",1,\"active\"],[\"memory_mb\",2,64],[\"launched_at\",4,\"2017-11-20T07:21:57\"],[\"instance_id\",1,\"940087d5-f889-4533-a5be-9574af5dd880\"],[\"host\",1,\"rdoceilosca.novalocal\"],[\"request_id\",1,\"req-1dd20f33-960b-4a23-99be-8f4984073839\"],[\"instance_type\",1,\"m1.mini\"],[\"project_id\",1,\"c679d411213b4ce086210d9412f2da90\"],[\"vcpus\",2,1]],\"message_signature\":\"5f26c9722f098ed123c601b0bc85ab080c003c2f88d8f3a762ace259acd66b24\",\"raw\":{},\"generated\":\"2017-12-18T02:29:14.895913\",\"message_id\":\"6a799ba5-b56a-47c2-8d72-5bc10b4b0dab\"}},\"previous\":\"alarm\"}";

        String json = "{\"severity\":\"low\",\"alarm_name\":\"alarm-event-image-delete\",\"current\":\"alarm\",\"alarm_id\":\"b980dc64-451c-4210-b522-07278c30eb73\",\"reason\":\"Event <id=488e137f-aa42-4440-8206-9ff38c76b009,event_type=image.delete> hits the query <query=[]>.\",\"reason_data\":{\"type\":\"event\",\"event\":{\"event_type\":\"image.delete\",\"traits\":[[\"status\",1,\"deleted\"],[\"deleted_at\",1,\"2017-12-18T06:25:02Z\"],[\"user_id\",1,\"c679d411213b4ce086210d9412f2da90\"],[\"name\",1,\"cirros-0.3.4\"],[\"service\",1,\"image.localhost\"],[\"resource_id\",1,\"7855ae0b-8461-435c-aebb-d19701e32fc5\"],[\"created_at\",1,\"2017-12-14T07:51:58Z\"],[\"project_id\",1,\"c679d411213b4ce086210d9412f2da90\"],[\"size\",1,\"13267968\"]],\"message_signature\":\"2974ad10ccce834a29b1c530574dd0df0186d7c9c92f5d6dcb7892a6068a8c02\",\"raw\":{},\"generated\":\"2017-12-18T06:25:02.912960\",\"message_id\":\"488e137f-aa42-4440-8206-9ff38c76b009\"}},\"previous\":\"alarm\"}";
        /**
         * ObjectMapper支持从byte[]、File、InputStream、字符串等数据的JSON反序列化。
         */
        ObjectMapper mapper = new ObjectMapper();
        EventModel eventModel = mapper.readValue(json, EventModel.class);
        System.out.println(eventModel.getAlarmName());
        System.out.println(eventModel.getReason());
        System.out.println(eventModel.getReasonData().getType());
        System.out.println(eventModel.getReasonData().getEvent().getEventType());
        System.out.println(eventModel.getReasonData().getEvent().getGenerated());
        System.out.println(eventModel.getReasonData().getEvent().getMessageId());
        System.out.println(eventModel.getReasonData().getEvent().getMessageSignature());
        System.out.println(eventModel.getReasonData().getEvent().getRaw());
        for (List<String> items: eventModel.getReasonData().getEvent().getTraits()) {
            for (String data : items) {
                System.out.print(data + ", ");
            }
            System.out.println();

        }
        System.out.println(eventModel.getSeverity());
        System.out.println(eventModel.getCurrent());
        System.out.println(eventModel.getPrevious());
    }

}
